<?php
App::uses('AppController', 'Controller');
/**
 * Places Controller
 *
 * @property Place $Place
 */
class PlacesController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Place->recursive = 0;
		$this->set('places', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Place->id = $id;
		if (!$this->Place->exists()) {
			throw new NotFoundException(__('Entrada invalida'));
		}
		$this->set('place', $this->Place->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Place->create();
			if ($this->Place->save($this->request->data)) {
				$this->Session->setFlash(__('El lugar ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El lugar no ha sido guardado, intente nuevamente.'));
			}
		}
		$pcategories = $this->Place->Pcategory->find('list');
		$this->set(compact('pcategories'));
	}

	public function add_place_pop($lat=null,$lng=null) {
                $this->layout = "clean";
		if ($this->request->is('post')) {
			$this->Place->create();
			if ($this->Place->save($this->request->data)) {
				$this->Session->setFlash(__('El lugar ha sido guardado'));
				$this->redirect(array('controller'=>'events','action' => 'add_event_pop',$this->Place->getInsertID()));
			} else {
				$this->Session->setFlash(__('El lugar no ha sido guardado, intente nuevamente.'));
			}
		}

		$pcategories = $this->Place->Pcategory->generateTreeList(null,null,null,' - ',null);
		$this->set(compact('pcategories'));
		$this->set(compact('lat'));
		$this->set(compact('lng'));

	}


	public function public_view() {
                $this->layout = "mapFormAddMobilePoint";
	}

	public function public_place_options($lat=null,$lng=null) {
                //$this->RequestHandler->isMobile()
                $this->layout = "publicPlaceOptions";
				$this->set(compact('lat'));
				$this->set(compact('lng'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Place->id = $id;
		if (!$this->Place->exists()) {
			throw new NotFoundException(__('Entrada invalida'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Place->save($this->request->data)) {
				$this->Session->setFlash(__('El lugar ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El lugar no ha sido guardado, intente nuevamente.'));
			}
		} else {
			$this->request->data = $this->Place->read(null, $id);
		}
		$pcategories = $this->Place->Pcategory->find('list');
		$this->set(compact('pcategories'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Place->id = $id;
		if (!$this->Place->exists()) {
			throw new NotFoundException(__('Entrada invalida'));
		}
		if ($this->Place->delete()) {
			$this->Session->setFlash(__('Lugar borrado'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('No se puede borrar'));
		$this->redirect(array('action' => 'index'));
	}
}
